#include <iostream>
#include <ctime>
// #include "CppDArray.h"
// #include "CppDLlist.h"
// #include "CppBTree.h"
#include "CppLinkTree.h"
#define LTree

void printint(void* ptr)
{
    long a = (long)ptr;
    std::cout<<a<<" ";
}

bool IsEqual(void* ptr1,void* ptr2)
{
    long p1 = (long)ptr1;
    long p2 = (long)ptr2;
    if(p1 == p2)
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool Compare_int(void* ptr1,void* ptr2)
{
    long p1 = (long)ptr1;
    long p2 = (long)ptr2;
    if(p1 > p2)
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool Sort_int(void* ptr1,void* ptr2)
{
    long a = (long)ptr1;
    long b = (long)ptr2;

    if(a < b)
    {
        return true;
    }
    else
    {
        return false;
    }
}
#ifdef BTree
int CompareByInt(void* ptr1,void* ptr2)
{
    long p1 = (long)ptr1;
    long p2 = (long)ptr2;
    if(p1 == p2)
    {
        return equal;
    }
    else if(p1 > p2)
    {
        return bigger;
    }
    else
    {
        return smaller;
    }
}
#endif
int main()
{
#ifdef DArray
    srand(time(NULL));
    DynamicArray a(NULL);
    for(long i=0;i<10;i++)
    {
        a.InsertArrayTail((void*)(i+1));
    }
    a.InsertArrayTail((void*)(10));
    a.InsertArrayTail((void*)(11));
    a.DisplayDynamicArray(printint);
    // a.Sort(Compare_int);
    // a.DisplayDynamicArray(printint);
    DynamicArray b = a.FindByElement((void*)10,IsEqual);
    b.DisplayDynamicArray(printint);
#endif

#ifdef DLlist
    srand(time(NULL));
    DoubleLinkList list(NULL);
    for(long i=0;i<10;i++)
    {
        list.InsertTail_DLList((void*)(rand()%50));
    }
    list.Travel_DLList(printint);
    list.DLListSort(Sort_int);
    list.Travel_DLList(printint);
#endif
#ifdef BTree 
    BinarySortTree tree;
    for(long i=0;i<10;i++)
    {
        tree.InsertElement_BSTree((void*)(i+1),CompareByInt);
    }

    tree.TravelMid_BSTree(printint);
    std::cout<<std::endl;

    tree.GetTreeRoot()->RemoveByElement(tree.GetTreeRoot(),(void*)3,CompareByInt);
    tree.TravelMid_BSTree(printint);
    long a = (long)tree.SearchByElement_BTNode((void*)5,CompareByInt)->GetNodeData();
    std::cout<<"\n"<<a<<std::endl;
#endif
#ifdef LTree
    LinkTree tree(NULL);
    TreeNode* node1 = new TreeNode((void*)1,NULL);
    TreeNode* node2 = new TreeNode((void*)2,NULL);
    TreeNode* node3 = new TreeNode((void*)3,NULL);
    node1->ConnectBranch(tree.GetTreeRoot());
    node2->ConnectBranch(node1);
    node3->ConnectBranch(node1);
    tree.TravelTree_LTree(printint);
    node1->DisconnectBranch(node1,node3);
    tree.TravelTree_LTree(printint);
    TreeNode* node =  tree.SearchNode_LinkTree((void*)2,IsEqual);
    long ret = (long)node->GetNodeData();
    std::cout<<ret<<std::endl;
    std::cout<<std::endl;
#endif
    return 0;
}